﻿using BLToolkit.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace CommonLibrary.Models
{
    public class CommandHelper
    {
        #region Properties
        private string selectCount;

        public string SelectCount
        {
            get { return selectCount; }
            set { selectCount = value; }
        }

        private string selectData;

        public string SelectData
        {
            get { return selectData; }
            set { selectData = value; }
        }

        private string body;

        public string Body
        {
            get { return body; }
            set { body = value; }
        }

        private string order;

        public string Order
        {
            get { return order; }
            set { order = value; }
        }

        private string limit;

        public string Limit
        {
            get { return limit; }
            set { limit = value; }
        }
        #endregion

        public CommandHelper()
        { 
        }

        public CommandHelper(
            string selectData,
            string body,
            string order = null,
            string limit = null,
            string selectCount = "select count(*) ")
        {
            this.selectCount = selectCount;
            this.selectData = selectData;
            this.body = body;
            this.order = order;
            this.limit = limit;
        }

        public void InitPage(int page, int pageSize)
        {
            if (page < 0 || pageSize <= 0)
            {
                this.limit = null;
                return;
            }

            this.limit =
                " limit " + pageSize.ToString() +
                " offset " + ((page - 1) * pageSize).ToString();
        }

        public void InitOrderBy(params KeyValuePair<string, bool>[] columns)
        {
            if (columns == null || columns.Length == 0)
            {
                this.order = null;
                return;
            }

            this.order = "order by ";
            this.order += columns[0].Key + (columns[0].Value ? " acs" : " desc");
            for (int i = 1; i < columns.Length; i++)
            {
                this.order += "," + columns[0].Key + (columns[0].Value ? " acs" : " desc");
            }
        }
        
        public string ToSelectCountCommand()
        {
            return string.Format(
                "{0} {1}",
                selectCount,
                body);
        }

        public string ToSelectDataCommand()
        {
            return string.Format(
                "{0} {1} {2} {3}",
                selectData,
                body,
                order,
                limit);        
        }

        public int ExecuteCount(DbManager db, params IDbDataParameter[] commandParameters)
        {
            return db.SetCommand(
                        this.ToSelectCountCommand(),
                        commandParameters)
                     .ExecuteScalar<int>();
        }

        public DataTable ExecuteDatatable(DbManager db, params IDbDataParameter[] commandParameters)
        {
            return db.SetCommand(
                        this.ToSelectDataCommand(),
                        commandParameters)
                     .ExecuteDataTable();
        }
        
        public override string ToString()
        {
            return string.Format(
                "{0} {1} {2} {3}",
                selectData,
                body,
                order,
                limit);
        }
    }
}
